package com.quxue.im.test;


import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.sql.Types;
import java.util.Collections;

@SpringBootTest
public class GeneratorCode {



    @Test
    public void testGeneratorCode(){

        FastAutoGenerator.create("jdbc:mysql://localhost:3306/quxue-im", "root", "root")
                .globalConfig(builder -> {
                    builder.author("jinken") // 设置作者
                            //.enableSwagger() // 开启 swagger 模式
                            .fileOverride() // 覆盖已生成文件
                            .outputDir("D:/趣学im"); // 指定输出目录
                })
                .dataSourceConfig(builder -> builder.typeConvertHandler((globalConfig, typeRegistry, metaInfo) -> {
                    int typeCode = metaInfo.getJdbcType().TYPE_CODE;
                    if (typeCode == Types.BIGINT) {
                        // 自定义类型转换
                        return DbColumnType.LONG;
                    }
                    return typeRegistry.getColumnType(metaInfo);

                }))
                .packageConfig(builder -> {
                    builder.parent("com.quxue") // 设置父包名
                            .moduleName("im") // 设置父包模块名
                            .pathInfo(Collections.singletonMap(OutputFile.xml, "D:/趣学IM")); // 设置mapperXml生成路径
                })
                .strategyConfig(builder -> {



                    builder.addInclude("chat_apply",
                                    "chat_collect",
                                    "chat_feedb",
                                    "chat_friend",
                                    "chat_group",
                                    "chat_group_info",
                                    "chat_msg",
                                    "chat_topic",
                                    "chat_topic_like",
                                    "chat_topic_reply",
                                    "chat_user",
                                    "chat_version")
                            .entityBuilder()
                            .idType(IdType.AUTO)
                    ;
                    ; // 设置需要生成的表名

                           // .addTablePrefix("t_"); // 设置过滤表前缀
                })
                .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                .execute();


    }

}
